package com.leo.service.impl;

import com.leo.annotation.Service;
import com.leo.annotation.Transaction;
import com.leo.bean.FileParam;
import com.leo.helper.DatabaseHelper;
import com.leo.helper.UploadHelper;
import com.leo.model.Customer;
import com.leo.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CustomerServiceImpl
 * @Description 提供客户数据服务
 * @Author wangss
 * @date 2019.12.16 19:18
 * @Version 1.0
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Override
    public List<Customer> getCustomerList() {

        return DatabaseHelper.queryEntityList(Customer.class, null, null);
    }

    @Transaction
    @Override
    public Customer getCustomer(long id) {
        Map<String, Object> fieldMap = new HashMap<>(1);
        fieldMap.put("id", id);
        return DatabaseHelper.queryEntity(Customer.class, null, fieldMap);
    }

    @Override
    public boolean createCustomer(Map<String, Object> fieldMap) {
        return DatabaseHelper.insertEntity(Customer.class, fieldMap);
    }

    /**
     * 创建客户
     */
    @Transaction
    @Override
    public boolean createCustomer(Map<String, Object> fieldMap, FileParam fileParam) {
        boolean result = DatabaseHelper.insertEntity(Customer.class, fieldMap);
        if (result) {
            UploadHelper.uploadFile("/tmp/upload/", fileParam);
        }
        return result;
    }

    @Override
    public boolean updateCustomer(long id, Map<String, Object> fieldMap) {
        return DatabaseHelper.updateEntity(Customer.class, id, fieldMap);
    }

    @Override
    public boolean deleteCustomer(long id) {
        return DatabaseHelper.deleteEntity(Customer.class, id);
    }
}
